package com.photoeditor.demo.util.font;

import android.app.Activity;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.widget.ImageView;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.db.DataManager;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.store.module.preload.PreLoadConfig;
import com.photoeditor.demo.model.store.module.preload.PreloadManager;
import com.photoeditor.demo.model.store.module.StoreOnlineBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by winn on 17/10/17.
 * 管理内置及在线字体，字体加载
 */

public class FontHelper {

    private static final String TAG = FontHelper.class.getSimpleName();

    public static final String DEFAULT_FONT_PKG_NAME = "DEFAULT";

    private static final String FONT_PKG_NAME_PREFIX = "com.photocollage.grid.font.plugins.";

    private ArrayList<StoreOnlineBean> mBuildFontList;

    private static FontHelper mInstance = new FontHelper();

    private FontHelper() {
        init();
    }

    public static FontHelper getInstance() {
        return mInstance;
    }

    private void init() {
        mBuildFontList = new ArrayList<>();
        mBuildFontList.addAll(loadBuildinFont());
    }

    public ArrayList<StoreOnlineBean> getFontList() {
        ArrayList<StoreOnlineBean> list = new ArrayList<>();
        list.addAll(mBuildFontList);
        PreloadManager preloadManager = PreloadManager.getInstance();
        StoreOnlineBean parent = preloadManager.getPreloadBean(PreLoadConfig.CONFIG_FONT_ID);
        if (parent != null && parent.getChildModules() != null) {
            list.addAll(parent.getChildModules());
        }
        return list;
    }

    public static Typeface loadFont(StoreOnlineBean bean) {
        Typeface ret = null;
        if (DEFAULT_FONT_PKG_NAME.equals(bean.getPkgName())) {
            ret = Typeface.DEFAULT;
        } else if (bean.isBuildin()) {
            ret = Typeface.createFromAsset(ComponentContext.getContext().getAssets(), bean.getAssetName());
        } else {
            if (hasDownload(bean)) {
                File file = new File(getLocalFilePath(bean));
                if (file.exists()) {
                    ret = Typeface.createFromFile(file);
                }
            }
        }
        return ret;
    }

    public static void loadFontIcon(StoreOnlineBean bean, ImageView imageView) {
        if (bean == null) {
            return;
        }
        if (bean.getResIconName() > 0) {
            imageView.setImageResource(bean.getResIconName());
        } else {
            if (imageView != null) {
                ImageLoader.loadImage((Activity) imageView.getContext(), bean.getPreview(), R.drawable.ic_default_image, imageView);
            }
        }
    }

    public static boolean hasDownload(StoreOnlineBean bean) {
        return bean.isBuildin() || !TextUtils.isEmpty(getLocalFilePath(bean));
    }

    private ArrayList<StoreOnlineBean> loadBuildinFont() {
        ArrayList<StoreOnlineBean> ret = new ArrayList<>();
        StoreOnlineBean bean = new StoreOnlineBean();
        bean.setPkgName(DEFAULT_FONT_PKG_NAME);
        bean.setBuildin(true);
        bean.setResIconName(R.drawable.ic_robot);
        ret.add(bean);
        bean = new StoreOnlineBean();
        bean.setPkgName(FONT_PKG_NAME_PREFIX + "blackopsone");
        bean.setBuildin(true);
        bean.setResIconName(R.drawable.ic_blackopsone);
        bean.setAssetName("fonts/BlackOpsOne-Regular.ttf");
        ret.add(bean);
        bean = new StoreOnlineBean();
        bean.setPkgName(FONT_PKG_NAME_PREFIX + "Righteous-Regular");
        bean.setBuildin(true);
        bean.setResIconName(R.drawable.ic_righteous);
        bean.setAssetName("fonts/Righteous-Regular.ttf");
        ret.add(bean);
        bean = new StoreOnlineBean();
        bean.setPkgName(FONT_PKG_NAME_PREFIX + "UnifrakturMaguntia-Book");
        bean.setBuildin(true);
        bean.setResIconName(R.drawable.ic_unifrakturmaguntia);
        bean.setAssetName("fonts/UnifrakturMaguntia-Book.ttf");
        ret.add(bean);
        bean = new StoreOnlineBean();
        bean.setPkgName(FONT_PKG_NAME_PREFIX + "MysteryQuest-Regular");
        bean.setBuildin(true);
        bean.setResIconName(R.drawable.ic_mystery);
        bean.setAssetName("fonts/MysteryQuest-Regular.ttf");
        ret.add(bean);
        bean = new StoreOnlineBean();
        bean.setPkgName(FONT_PKG_NAME_PREFIX + "CabinSketch-Bold");
        bean.setBuildin(true);
        bean.setResIconName(R.drawable.ic_cabin);
        bean.setAssetName("fonts/CabinSketch-Bold.ttf");
        ret.add(bean);
        return ret;
    }

    public static String getLocalFilePath(StoreOnlineBean bean) {
        String ret = "";
        ret = DataManager.getInstance().getNormalResFile(bean.getPkgName());
        if (new File(ret).exists()) {
            return ret;
        }
        ret = "";
        PreloadManager preloadManager = PreloadManager.getInstance();
        StoreOnlineBean parent = preloadManager.getPreloadBean(PreLoadConfig.CONFIG_FONT_ID);
        if (parent != null && parent.getChildModules() != null) {
            List<StoreOnlineBean> list = parent.getChildModules();
            for (StoreOnlineBean child : list) {
                if (child.getPkgName() != null
                        && child.getPkgName().equals(bean.getPkgName())) {
                    String url = child.getDownUrl();
                    ret = preloadManager.getPreloadFile(url);
                    if (!TextUtils.isEmpty(ret) && new File(ret).exists()) {
                        return ret;
                    }
                    break;
                }
            }
        }
        return ret;
    }
}
